import wx from "weixin-js-sdk"
import axios from 'axios'

function weixinShare(article, openid, oldId, userArticleId, flags) {

  var href = window.location.href.split('#')[0];
  const that = this;
  axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
  var params = new URLSearchParams();
  params.append('url', href)
  axios({
      method: "get",
      url: 'https://yokapp.mynatapp.cc/wechat/share/',
      params: {
        url: href,
      }
    })
    .then(function(res) {
      wxshare(res.data, "");
    })
    .catch(function(err) {
      console.log(err);
    });

  function wxshare(data, url) {
    let lhref = location.href;
    if (lhref.search('code') != -1) {
      lhref = 'https://yok.mynatapp.cc/#' + lhref.split('#')[1];;
    }
    var url = lhref; //分享的文章地址
    var appId = data.appId;
    var timestamp = data.timestamp;
    var nonceStr = data.nonceStr;
    var signature = data.signature;
    wx.config({
      beta: true,
      debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
      appId: appId, // 必填，公众号的唯一标识
      timestamp: timestamp, // 必填，生成签名的时间戳
      nonceStr: nonceStr, // 必填，生成签名的随机串
      signature: signature, // 必填，签名，见附录1
      jsApiList: ['onMenuShareAppMessage', 'onMenuShareTimeline']
    });

    wx.ready(function() {
      //分享朋友圈
      wx.onMenuShareTimeline({
        title: article.title, // 分享标题
        desc: article.describe, // 分享描述
        link: url, // 分享链接
        imgUrl: article.imgUrl, // 分享图标
        success: function() {
          axios.get('https://yokapp.mynatapp.cc/article/share', {
              params: {
                articleId: article.id,
                title: article.title,
                imgUrl: article.imgUrl,
                openid: openid,
                userArticleId: userArticleId,
                oldId: oldId,
                flag: flags
              },
            })
            .then(res => {
              console.log('res=>', res);
            })
            .then(err => {
              console.log(err);
            })
          console.log("分享成功,url:" + url)
        },
        cancel: function() {
          // 用户取消分享后执行的回调函数
          console.log("分享失败")
        }
      });

      wx.onMenuShareAppMessage({
        title: article.title, // 分享标题
        desc: article.describe, // 分享描述
        link: url, // 分享链接
        imgUrl: article.imgUrl, // 分享图标
        type: "", // 分享类型,music、video或link，不填默认为link
        dataUrl: "", // 如果type是music或video，则要提供数据链接，默认为空
        success: function() {
          axios.get('https://yokapp.mynatapp.cc/article/share', {
              params: {
                articleId: article.id,
                title: article.title,
                imgUrl: article.imgUrl,
                openid: openid,
                userArticleId: userArticleId,
                oldId: oldId,
                flag: flags
              },
            })
            .then(res => {
              console.log('res=>', res);
            })
            .then(err => {
              console.log(err);
            })
          console.log("分享成功,url:" + url)
        },
        cancel: function() {
          // 用户取消分享后执行的回调函数
          console.log("分享失败")
        }
      });
    });
  }
}

export default weixinShare
